package com.mdlib.helper.ui.fragments.sect.send;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ljlib.core.base.BaseFragment;
import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.UserEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.GlideCircleTransform;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/15 14:50
 * 邮箱：hqy_xz@126.com
 */

public class SendMeFragment extends BaseFragment {


    @Bind(R.id.rl_me_phoen_close_tips)
    RelativeLayout mRlMePhoenCloseTips;
    @Bind(R.id.rl_phone_tips)
    RelativeLayout mRlPhoneTips;

    @Bind(R.id.tv_me_nickname)
    TextView mTvMeNickname;
    @Bind(R.id.ll_me_edit)
    LinearLayout mLlMeEdit;
    @Bind(R.id.rl_me_address)
    RelativeLayout mRlMeAddress;
    @Bind(R.id.rl_me_phone)
    RelativeLayout mRlMePhone;
    @Bind(R.id.rl_me_balance)
    RelativeLayout mRlMeBalance;
    @Bind(R.id.rl_me_voucher)
    RelativeLayout mRlMeVoucher;
    @Bind(R.id.rl_me_share)
    RelativeLayout mRlMeShare;
    @Bind(R.id.rl_me_message)
    RelativeLayout mRlMeMessage;
    @Bind(R.id.rl_me_problem)
    RelativeLayout mRlMeProblem;
    @Bind(R.id.rl_me_set)
    RelativeLayout mRlMeSet;
    @Bind(R.id.rl_me_run)
    RelativeLayout mRlMeRun;
    @Bind(R.id.tv_send_me_phone)
    TextView mTvSendMePhone;
    @Bind(R.id.iv_send_me_userface)
    ImageView mIvSendMeUserface;

    private int isRun;
    private UserEntity userInfo;

    public static SendMeFragment newInstance() {
        return new SendMeFragment();
    }


    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_send_me;
    }

    @Override
    public void onStart() {
        super.onStart();
        getUserInfo();
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .fetchUserInfo()
                .compose(RxHelper.<BaseResult<UserEntity>>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult<UserEntity>>() {
                    @Override
                    protected void onSucc(BaseResult<UserEntity> userEntity) {
                        stopLoadingDialog();
                        userInfo = userEntity.data;
                        isRun = userInfo.getIsRun();
                        if (isRun == 1) {
                            mRlMeRun.setVisibility(View.GONE);
                        }
                        mTvMeNickname.setText(userInfo.getNickName());
                        mTvSendMePhone.setText(userInfo.getPhone());
                        if (EmptyUtils.isNotEmpty(userInfo.getPhone())) {
                            mRlPhoneTips.setVisibility(View.GONE);
                        } else {
                            mRlPhoneTips.setVisibility(View.VISIBLE);
                        }
                        Glide.with(getActivity())
                                .load(userInfo.getAvatarUrl())
                                .asBitmap()
                                .placeholder(R.drawable.iv_app_default_head)
                                .error(R.drawable.iv_app_default_head)
                                .transform(new GlideCircleTransform(getActivity()))
                                .into(mIvSendMeUserface);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        stopLoadingDialog();
                    }
                }));
    }


    @OnClick({R.id.rl_me_phoen_close_tips, R.id.rl_me_run, R.id.ll_me_edit, R.id.rl_me_address,
            R.id.rl_me_phone, R.id.rl_me_balance, R.id.rl_me_voucher, R.id.rl_me_share, R.id.rl_me_message,
            R.id.rl_me_problem, R.id.rl_me_set, R.id.rl_me_buy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_me_phoen_close_tips:
                mRlPhoneTips.setVisibility(View.GONE);
                break;

            case R.id.ll_me_edit:
                UIHelper.showProfileEdit(getActivity());
                break;
            case R.id.rl_me_run:
                if (isRun == 0) {
                    UIHelper.showRun(getActivity());
                }
                break;
            case R.id.rl_me_address:
                UIHelper.showAddress(getActivity(), UIHelper.ADDRESS_TYPE_TWO);
                break;
            case R.id.rl_me_phone:
                if (EmptyUtils.isEmpty(userInfo.getPhone())) {
                    UIHelper.showBindPhonePage(getActivity());
                }
                break;
            case R.id.rl_me_balance:
                UIHelper.showBalance(getActivity());
                break;
            case R.id.rl_me_buy:
                UIHelper.showMyBuy(getActivity());
                break;
            case R.id.rl_me_voucher:
                UIHelper.showVoucher(getActivity());
                break;
            case R.id.rl_me_share:
//                addFragment(SendShareFragment.newInstance());
                UIHelper.showShare(getActivity());
                break;
            case R.id.rl_me_message:
                UIHelper.showMessage(getActivity());
                break;
            case R.id.rl_me_problem:
                UIHelper.showProblem(getActivity());
                break;
            case R.id.rl_me_set:
                UIHelper.showSet(getActivity());
                break;
        }
    }


}
